home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sunsite.doc.ic.ac.uk!warwick!bsmail!talisker!nathan
- From: nathan@pact.srf.ac.uk (Nathan Sidwell)
- Subject: Re: is this string a number?
- Message-ID: <DL9tzD.H0@uns.bris.ac.uk>
- Sender: usenet@uns.bris.ac.uk (Usenet news owner)
- Nntp-Posting-Host: talisker.pact.srf.ac.uk
- Organization: Inmos
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4dbogk$763@jupiter.planet.net> <30f9ac87.195826304@nntp.ix.netcom.com>
- Date: Tue, 16 Jan 1996 11:16:25 GMT
-
- Mike Rubenstein (miker3@ix.netcom.com) wrote:
- : Chris Kemp <chrisk@paladn.com> wrote:
-
- : |>I am inputting a string from the keyboard from a user, and
- : |>intend to use the atol or strtol functions to convert the
- : |>string to a number.
-
- : Use strtol() and supply a non-null second argument. When strtol()
- : returns, the pointer will point to the first character that was not
- : converted. Example:
-
- : #include <stdlib.h>
-
- : char *str;
- : char *nstr;
- : long l;
-
- : /* ... */
-
- : l = strtol(str, &nstr, 10);
- : if (nstr == str || *nstr != '\0')
- : {
- : /* str was empty or there is an illegal character */
- : }
-
- strtol et al remove leading white space, thus if the string " " is supplied
- the above check won't tell if the string was blank (nstr and str will
- be different and *nstr will be 0). Depending on
- your application this may or may not be a problem.
-
- nathan
- --
- Nathan Sidwell Holder of the Xmris home page
- Chameleon Architecture Group at SGS-Thomson, formerly Inmos
- http://www.pact.srf.ac.uk/~nathan/ Tel 0117 9707182
- nathan@inmos.co.uk or nathan@bristol.st.com or nathan@pact.srf.ac.uk
- Having problems? try http://www.pact.srf.ac.uk/~nathan/problems/
-